package com.kilomob.grady.handler.netty;

import com.kilomob.grady.model.Request;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

/**
 * 
 * 
 *   
 * @author zhouzhixiang  
 * @date 2017年4月6日
 * @since 1.0
 */
@ChannelHandler.Sharable
public class ConnectorIdleStateTrigger extends ChannelInboundHandlerAdapter {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleState state = ((IdleStateEvent) evt).state();
            if (state == IdleState.WRITER_IDLE) {
                // write heartbeat to server
                Request req = new Request();
                req.setClassName("xxx.xxx.HeardBeat");
                req.setMessageId("llllllllllll");
                ctx.writeAndFlush(req);
            }
        } else {
            super.userEventTriggered(ctx, evt);
        }
    }
}
